package com.su.servlet;

import com.su.bean.User;
import com.su.service.UserService;

import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.annotation.*;
import java.io.IOException;
import java.util.Date;

@WebServlet(name = "UpdateUserInfoServlet", value = "/UpdateUserInfoServlet")
public class UpdateUserInfoServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("UTF-8");
        response.setContentType("text/html;charset=UTF-8");
        if (request.getParameter("id").equals("1")) {
            String userName = request.getParameter("userName");
            String userSex = request.getParameter("userSex");
            String userAdd = request.getParameter("userAdd");
            int userId = Integer.valueOf(request.getParameter("userId"));
            System.err.println(userName);
            System.err.println(userSex);
            System.err.println(userAdd);
            System.err.println(request.getParameter("userId"));
            User user = new User();
            user.setId(userId);
            user.setUsername(userName);
            user.setBirthday(new Date());
            user.setSex(userSex);
            user.setAddress(userAdd);
            UserService userService = new UserService();
            System.out.println(user);
            userService.updateUserInfo(user);
            request.getRequestDispatcher("/userInfoServlet").forward(request, response);
        } else {
            String id = request.getParameter("id");
            UserService userService = new UserService();
            User userInfoById = userService.getUserInfoById(Integer.valueOf(id));
            request.setAttribute("userInfoById", userInfoById);
            System.out.println("指定id:" + id + " 查询出的user信息" + userInfoById);
            request.getRequestDispatcher("/updatePanel.jsp").forward(request, response);
        }
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doGet(request, response);
    }
}
